package com.yys.huarongdao.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.RenderNode;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yys.huarongdao.utils.BgmUtil;

import java.util.Timer;
import java.util.TimerTask;


public class MyApplication extends Application {

    private static MyApplication application;
    public static int RANK_3X3 =3;
    public static int RANK_4X4=4;
    public static int RANK_5x5=5;
    private int rankType;
    private String account;

    private volatile int activityResumedCount=0;

    public static MyApplication getApplication(){
        return MyApplication.application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application=this;
        registerActivityLifecycleCallbacks(new MyActivityLifecycleCallbacks());
    }



    public void setRankType(Context context,int rankType){
        this.rankType=rankType;
        context.getSharedPreferences("rankType",MODE_PRIVATE).edit().putInt("rankType",rankType).commit();
    }

    public int getRankType(Context context){
        //如果application销毁了重建后没有数据，则从文件中取
        if(this.rankType==0){
            this.rankType=context.getSharedPreferences("rankType",MODE_PRIVATE).getInt("rankType",3);
        }
        return this.rankType;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    private void backToApp(Activity activity){
        if(BgmUtil.getBgmStatus()==BgmUtil.STATUS_BGM_PAUSE)
            BgmUtil.openBgm(this);
//        Log.d("BGM","MyApplication，返回app"+(BgmUtil.getBgmStatus()==BgmUtil.STATUS_BGM_PAUSE));
    }

    private void leaveApp(final Activity activity){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                if(activityResumedCount<1){
                    if(BgmUtil.BgmPlaying())
                        BgmUtil.pauseBgm(activity);
                }
            }
        },500);
    }

    class MyActivityLifecycleCallbacks implements ActivityLifecycleCallbacks{
        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            activityResumedCount++;
            if(activityResumedCount>=1){
                backToApp(activity);
            }
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            activityResumedCount--;
            if(activityResumedCount==0){
                leaveApp(activity);
            }
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    }

}
